# -*- coding:utf-8 -*-
__author__ = 'shuxin'
__date__ = '2020/6/15  14:11'

import copy
import hashlib
import json
import urllib.parse
from functools import reduce


def test():
    # system_params = {
    #     "createTime": "1411442340",
    #     "key": "precise_order_export",
    #     "params": {"orderNo": "precise_order_export"},
    #     "tag": "flight.order.orderInfo.orderDetail",
    #     "token": "precise_order_export"  # QAE 分配给用户的 token，通过授权获取
    # }
    dict_str = {
        "createTime": "1506338970415",
        "key": 'cae72d9d232b9e747247cec4d8cd5881',
        "params": {"dpt": "PEK", "arr": "SHA", "date": "2017-08-30", "ex_track": "tehui"},
        "tag": "flight.national.supply.sl.searchflight",
        "token": "5813b8dca765aecff246b1aaf12344ec"
    }
    dict_str_sign = copy.deepcopy(dict_str)
    dict_str_sign['params'] = json.dumps(dict_str_sign['params'], separators=(',', ':'))
    reverse_dict_str = sorted(dict_str_sign.items(), key=lambda k: k[0], reverse=False)
    res = reduce(lambda x, y: x + y, map(lambda x: '{}={}'.format(*x), reverse_dict_str))
    # res='createTime=1411442340key=testparams={"orderNo":"precise_order_export"}tag=flight.order.orderInfo.orderDetailtoken=precise_order_export'
    # md5加密
    m = hashlib.md5()
    m.update(res.encode("utf8"))
    print(urllib.parse.quote(dict_str_sign['params']))
    print(urllib.parse.quote('{}'))
    print(m.hexdigest())
    return m.hexdigest()


def test2():
    data = [{
        "orderNo": "5ee638b24614a4d3ed0b98f4",
        "subOrderNos": ["1111111111", "222222222222"]
    }, {
        "orderNo": "5ee714f3e26e18d5433f5be2",
        "subOrderNos": ["3333333333", "44444444444"]
    }]
    new_dict = {}

    def comb_dict(x):
        new_dict[str(x['orderNo'])] = x['subOrderNos']
        return new_dict

    list(map(lambda x: comb_dict(x), data))


def ergodic_dict(src, dct=None):
    if dct == None:
        dct = {}
    for k, v in src.items():
        if isinstance(v, (list, tuple, set, dict)):
            ergodic_dict(v, dct)
        else:
            dct[k] = v
    return dct


if __name__ == '__main__':
    data = {"record":
        {
            "ckno": "5715704468924Luggage1",
            "serviceType": 2,
            "saleSource": 1,
            "ckstatus": 0,
            "detail": "15kg",
            "relation": {
                "otaOrderNo": "test180702204657186e34c5",
                "subOrderNo": "5715704468924",
                "userOrderNo": "331585852194"
            },
            "fOrderStatus": 1,
            "serviceProvider": 0
        }
    }
    res = ergodic_dict(data)
    print(res)
    {'ckno': '5715704468924Luggage1',
     'serviceType': 2,
     'saleSource': 1,
     'ckstatus': 0,
     'detail': '15kg',
     'otaOrderNo': 'test180702204657186e34c5',
     'subOrderNo': '5715704468924',
     'userOrderNo': '331585852194',
     'fOrderStatus': 1,
     'serviceProvider': 0}


